package com.foreknow.elm.dao.impl;

import com.foreknow.elm.dao.OrdersDao;
import com.foreknow.elm.po.Business;
import com.foreknow.elm.po.Orderdetailet;
import com.foreknow.elm.po.Orders;
import com.foreknow.elm.util.CommonUtil;
import com.foreknow.elm.util.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class OrdersDaoImpl implements OrdersDao {
    private Connection con = null;
    private PreparedStatement pst = null;
    private ResultSet rs = null;

    @Override
    public int createOrders(Orders orders) throws Exception {
        int orderId = 0;
        String sql = "insert into orders(userId,businessId,orderDate,orderTotal,daId,orderState) value(?,?,?,?,?,?)";
        try {
            con = DBUtil.getConnection();
            pst = con.prepareStatement(sql, PreparedStatement.RETURN_GENERATED_KEYS);
            pst.setString(1, orders.getUserId());
            pst.setInt(2, orders.getBusinessId());
            pst.setString(3, CommonUtil.getCurrentDate());
            pst.setDouble(4, orders.getOrderTotal());
            pst.setInt(5, orders.getDaId());
            pst.setInt(6, orders.getOrderState());
            pst.executeUpdate();
            rs = pst.getGeneratedKeys();
            while (rs.next()) {
                orderId = rs.getInt(1);
            }
        } finally {
            DBUtil.close(rs, pst);
        }
        return orderId;
    }

    @Override
    public Orders getOrdersById(Integer orderId) throws Exception {
        Orders orders = new Orders();
        List<Orderdetailet> orderdetailets = new ArrayList<>();
        String sql = "select * from orders o,business b,orderdetailet od " +
                " where o.businessId = b.businessId and o.orderId = od.orderId and" +
                " o.orderId=?";
        con = DBUtil.getConnection();
        pst = con.prepareStatement(sql);
        pst.setInt(1,orderId);
        rs = pst.executeQuery();
        while (rs.next()){
            // 订单
            orders.setOrderId(rs.getInt("orderId"));
            orders.setUserId(rs.getString("userId"));
            orders.setBusinessId(rs.getInt("businessId"));
            orders.setOrderDate(rs.getString("orderDate"));
            orders.setOrderTotal(rs.getDouble("orderTotal"));
            orders.setDaId(rs.getInt("daId"));
            orders.setOrderState(rs.getInt("orderState"));
            // 商家
            Business business = new Business();
            business.setBusinessId(rs.getInt("businessId"));
            business.setBusinessName(rs.getString("businessName"));
            business.setBusinessAddress(rs.getString("businessAddress"));
            business.setBusinessExplain(rs.getString("businessExplain"));
            business.setBusinessImg(rs.getString("businessImg"));
            business.setOrderTypeId(rs.getInt("orderTypeId"));
            business.setStarPrice(rs.getDouble("starPrice"));
            business.setDeliveryPrice(rs.getDouble("deliveryPrice"));
            business.setRemarks(rs.getString("remarks"));
            orders.setBusiness(business);
            // 订单详情
            Orderdetailet orderdetailet = new Orderdetailet();
            orderdetailet.setOdId(rs.getInt("odId"));
            orderdetailet.setOrderId(orderId);
            orderdetailet.setFoodId(rs.getInt("foodId"));
            orderdetailet.setQuantity(rs.getInt("quantity"));
            orderdetailets.add(orderdetailet);
            orders.setList(orderdetailets);
        }
        return orders;
    }

    @Override
    public List<Orders> listOrdersByUserId(String userId) throws Exception {
        String sql = "select o.*,b.*,od.* from  orders o,business b,orderdetailet od where" +
                " o.orderId = od.orderId and o.businessId = b.businessId" +
                " and o.userId = ?";
        List<Orders> list = new ArrayList<>();
        try {
            con = DBUtil.getConnection();
            pst = con.prepareStatement(sql);
            pst.setString(1,userId);
            rs = pst.executeQuery();
            while (rs.next()) {
                Orders orders = new Orders();
                orders.setOrderId(rs.getInt("orderId"));
                orders.setUserId(rs.getString("userId"));
                orders.setBusinessId(rs.getInt("businessId"));
                orders.setOrderDate(rs.getString("orderDate"));
                orders.setOrderTotal(rs.getDouble("orderTotal"));
                orders.setDaId(rs.getInt("daId"));
                orders.setOrderState(rs.getInt("orderState"));
                // 商家
                Business business = new Business();
                business.setBusinessId(rs.getInt("businessId"));
                business.setBusinessName(rs.getString("businessName"));
                business.setBusinessAddress(rs.getString("businessAddress"));
                business.setBusinessExplain(rs.getString("businessExplain"));
                business.setBusinessImg(rs.getString("businessImg"));
                business.setOrderTypeId(rs.getInt("orderTypeId"));
                business.setStarPrice(rs.getDouble("starPrice"));
                business.setDeliveryPrice(rs.getDouble("deliveryPrice"));
                business.setRemarks(rs.getString("remarks"));
                orders.setBusiness(business);
                // 订单详情
                Orderdetailet orderdetailet = new Orderdetailet();
                orderdetailet.setOdId(rs.getInt("odId"));
                orderdetailet.setOrderId(rs.getInt("orderId"));
                orderdetailet.setFoodId(rs.getInt("foodId"));
                orderdetailet.setQuantity(rs.getInt("quantity"));
                list.add(orders);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }
}
